package offer.offer01;

import java.util.Stack;

/**
 *
 */
public class S09两个栈实现队列 {
    // 自己写的, 用时很长, 毫无优化, 结果5, 100;
    private Stack<Integer> stack1; //存储入栈的元素
    private Stack<Integer> stack2; //出栈时做中转

    public void CQueue() {
        stack1 = new Stack();
    }

    public void appendTail(int value) {
        stack1.push(value);
    }

    public int deleteHead() {
        if(stack1.empty()){
            return -1;
        }
        int tmp1 = 0;
        stack2 = new Stack();
        while(!stack1.empty()){
            tmp1 = stack1.pop();
            stack2.push(tmp1);
        }
        int result = stack2.peek();
        int tmp2 = 0;
        while(!stack2.empty()){
            tmp2 = stack2.pop();
            stack1.push(tmp2);
        }
        return result;
    }
}
//根据网上解答, 结果67, 100; 两个堆栈其实就是头和尾的关系, 这样可以避免重复转移堆栈元素
class CQueue {
    private Stack<Integer> stack1; //存储入栈的元素
    private Stack<Integer> stack2; //出栈时做中转

    public CQueue() {
        stack1 = new Stack();
        stack2 = new Stack();
    }
    public void appendTail(int value) {
        stack1.push(value);
    }
    public int deleteHead() {
        if(stack1.empty() && stack2.empty()){
            return -1;
        }else{
            if(!stack2.empty()){
            }else{
                while(!stack1.empty()){
                    stack2.push(stack1.pop());
                }
            }
            return stack2.pop();
        }
    }
}
